<?php
/**
 * 管理员管理
 * 跃飞科技版权所有 @2017
 * User: 钟贵廷
 * Date: 2017/5/23
 * Time: 15:48
 */

namespace app\admin\controller;
use think\Db;
use think\Loader;
use think\Request;

class AdminMember extends Admin
{
    public function lst()
    {
        $data['adminMemberData'] = db('AdminMember')->select();
        return $this->fetch('lst',$data);
    }

    /**
     * 添加管理员
     */
    public function add()
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('AdminMember');
            $adminMemberInfo = input('post.');
            if($validate->check($adminMemberInfo))
            {
                $adminMemberModel = new \app\admin\model\AdminMember();
                $data['username'] = $adminMemberInfo['username'];
                $data['salt'] = $adminMemberModel->getSalt();
                $data['password'] = $adminMemberModel->my_md5($adminMemberInfo['password'],$data['salt']);
                Db::name('admin_member')->insert($data);
                $admin_member_id = Db::name('admin_member')->getLastInsID();
                if( $admin_member_id > 0)
                {
                    $this->success('添加成功', url('lst'));
                }
                else
                {
                    $this->success('添加失败');
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }
        return  $this->fetch();
    }


    public function save($id)
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('AdminMember');
            $adminMemberInfo = input('post.');
            if($validate->check($adminMemberInfo))
            {
                $adminMemberModel = new \app\admin\model\AdminMember();
                if(trim($adminMemberInfo['password']) == '')
                {
                    unset($adminMemberInfo['password']);
                }
                else
                {
                    $data['salt'] = $adminMemberModel->getSalt();
                    $data['password'] = $adminMemberModel->my_md5($adminMemberInfo['password'],$data['salt']);
                    $res = Db::name('admin_member')
                        ->where('id',$adminMemberInfo['id'] )
                        ->update($data);
                    if($res)
                    {
                        $this->success('编辑成功', url('lst'));
                    }
                    else
                    {
                        $this->success('编辑失败');
                    }
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }
        $data['adminMemberData'] = db('AdminMember')->find($id);
        return  $this->fetch('',$data);
    }


    public function delete($id)
    {
        $adminMember = db('AdminMember');
        $admin_data =  $adminMember->find($id);
        if(empty($admin_data))
        {
            $this->error('手贱，滚！');
        }
        else
        {
            if($admin_data['username'] == 'admin')
            {
                $this->error('admin超级管理员不能删');
            }
            else
            {
                $adminMember->delete($id);
                $this->success('删除成功！');
            }
        }
    }
}